<template>
  <div>
    <tableComponents
      :pagination="{ currentPage, pageSize, total: list.all_num }"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="receive_search"
      :isHaveExport="false"
      :isHaveAdd="true"
      @toAdd="add_data"
      :isShow="true"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list"
          v-loading="loading"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.9%"
        >
          <!-- <el-table-column type="selection" class="tc" width="35"></el-table-column> -->

          <el-table-column label="字段信息" min-width="80">
            <template slot-scope="scope">
              <div class="user_table">
                <span>字段名称：</span>
                <small>{{ scope.row.title || 0 }}</small>
                <br />
                <span>英文标识：</span>
                <small>{{ scope.row.iden || 0 }}</small>
                <br />
              </div>
            </template>
            <!-- <template slot-scope="scope">{{ scope.row.title }}</template> -->
          </el-table-column>

          <el-table-column label="信息" min-width="160">
            <template slot-scope="scope">
              <div>
                <p>表单类型：{{ toTypes(scope.row.types) }}</p>
                <p>备注：{{ scope.row.remark || '---'}}</p>
                <p>默认文字：{{ scope.row.default|| '---' }}</p>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="参数" min-width="80">
            <template slot-scope="scope">{{ scope.row.parameter|| '---' }}</template>
          </el-table-column>

          <el-table-column prop="name" label="是否开启" min-width="80">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.is_open"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="开启"
                inactive-text="关闭"
                active-value="1"
                @change="getOpen($event,scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="name" label="是否必填" min-width="80">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.is_verification"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="是"
                inactive-text="不是"
                active-value="1"
                @change="getVerification($event,scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="添加时间" min-width="100">
            <template slot-scope="scope">
              <div class="user_table">
                <span>添加时间：</span>
                <small>{{ scope.row.created_time | getTimeFormat}}</small>
                <br />
                <span>更新时间：</span>
                <small>{{ scope.row.update_time | getTimeFormat}}</small>
                <br />
              </div>
            </template>
            <!-- <template slot-scope="scope">{{ scope.row.created_time | getTimeFormat }}</template> -->
          </el-table-column>
          <!-- <el-table-column label="更新时间" min-width="100">
            <template slot-scope="scope">{{ scope.row.update_time | getTimeFormat }}</template>
          </el-table-column>-->

          <el-table-column label="操作" align="center" min-width="120">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                @click="seetype(scope.row,false)"
                size="mini"
                type="primary"
              >查看</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="checkBd(false,scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="deleteNew(scope.$index, scope.row.id)"
                type="danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>
    <el-dialog
      title="编辑字段管理"
      :close-on-click-modal="false"
      class="tc"
      :append-to-body="true"
      :visible.sync="showBdFrom"
      width="800px"
      center
    >
      <fieldForm
        v-if="showBdFrom"
        @closedia="showBdFrom=false"
        @hideDio="getHideDio"
        :clickType="atype"
        :row="row"
      />
    </el-dialog>
  </div>
</template>
<script>
import HeadTitle from "@/components/currency/HeadTitle.vue";
import fieldForm from "../../plugin/character/field-form.vue";
export default {
  data() {
    return {
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      list: [],
      loading: true,
      showBdFrom: false,
      row: {},
      avatarImg: "",
      atype: 0, // false标识查看 true表示修改
    };
  },
  components: {
    HeadTitle,
    fieldForm,
  },
  methods: {
    seetype(row) {
      this.atype = 0;
      this.row = row;
      this.showBdFrom = true;
    },
    add_data() {
      this.atype = 1;
      this.row = {};
      this.showBdFrom = true;
    },
    toTypes(type) {
      if (type == "1") {
        return "文本框";
      } else if (type == "2") {
        return "单选";
      } else if (type == "3") {
        return "多选";
      } else if (type == "4") {
        return "下拉";
      } else if (type == "5") {
        return "时间";
      } else if (type == "6") {
        return "图片";
      } else if (type == "7") {
        return "文本域";
      }
    },
    // openDetails1(id) {
    //   this.$router.push({
    //     path: "/index/admin/characterMsg",
    //     query: { id: id, page: 1 },
    //   });
    //   // window.open(routeData.href, "_blank");
    // },
    getHideDio(bool, pic) {
      this.showBdFrom = false;
      if (bool) {
        this.getNewsAll();
      }
      this.avatarImg = pic;
    },
    checkBd(bool, row) {
      if (bool) {
        this.row = {};
      } else {
        this.row = row;
      }
      this.atype = 1;
      this.showBdFrom = true;
    },
    getOpen(val, row) {
      var params = {};
      params = row;
      params.is_open = val;
      this.updata(params);
    },
    getVerification(val, row) {
      var params = {};
      params = row;
      params.is_verification = val;
      this.updata(params);
    },
    updata(row, bool) {
      var params = {};
      params = row;
      this.$api.getCharacter.characterFieldSaveEdit(params).then((res) => {
        if (res.data.status == 1 && res.data.code == 200) {
          // if (bool) {
          this.$message.success("提交成功");
          // }
          //  this.$message.success('提交成功');
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    getNewsAll() {
      var id = this.$route.query.id || "";
      this.$api.getCharacter.getCharacterFieldList({ id }).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
          for (let i = 0; i < this.list.length; i++) {
            if (this.list[i].types === "6") {
              this.list[i].parameter = this.avatarImg;
            }
          }
        }
        this.loading = false;
      });
    },
    deleteNew(index, id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          var data = { id: id };
          this.$api.getCharacter.characterFieldDel(data).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.list.splice(index, 1);
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },
    handleCurrentChange() {},
    handleSizeChange() {},
    receive_search() {},
  },

  created() {
    // this.$store.dispatch("getCharacterFieldList");
    this.getNewsAll();
  },
  computed: {},
};
</script>

<style lang="less" scoped>
thead > tr > th {
  height: 45px;
  line-height: 45px;
}
td,
th {
  border: 1px solid #e1e3e4;
  font-size: 12px;
}
th {
  background: #f8f8f8;
  font-weight: normal;
  color: #323437;
}
td {
  height: 75px;
  color: #000;
}
.caozuo {
  padding-left: 50px;
}
.pl {
  padding-left: 15px;
}
.chkBox {
  display: block;
  margin: 0 auto;
  width: 18px;
  height: 18px;
  background: #fff;
}
.aBtn_1 {
  padding: 10px;
}
</style>
